maven_project_dir=/var/jenkins_home/workspace/cicd-demo/
dockerfile_dir=/var/jenkins_home/deployments/docker/dev/
host_port=10000
container_port=8080
container_name=cicd-demo
image_name=cicd-demo
echo "------------------------------------------------------------begin to build image----------------------------------------------------------------------------------------------------"
## maven 编译打包项目
## echo "------------------------------------------------------------Maven build package project---------------------------------------------------------------------------------------------"
## jenkins已经编译了项目，这里无须再调用mvn命令构建
## cd $maven_project_dir
## mvn clean package surefire-report:report -Dmaven.test.skip=false
echo "------------------------------------------------------------Docker build image-----------------------------------------------------------------------------------------------------"
cd $maven_project_dir/target
cp *.jar $dockerfile_dir
## 切换到dockerfile所在目录
cd $dockerfile_dir
## 生成cicd-demo镜像
docker build -t cicd-demo .
echo "-----------------------------------------------------------Build image successfully------------------------------------------------------------------------------------------------"
## 查看对应容器是否正在运行
# container_id=`docker ps --filter name=cicd-demo --filter status=running --format "{{.ID}}"`
container_id=`docker ps -a --filter name=$container_name --format "{{.ID}}"`
if [ $container_id ]; then
   echo "------------------------------------------------------Container is running or container name exist,now to stop and remove it-----------------------------------------------------"
   ## 停止正在运行的容器
   docker stop $container_id
   docker rm $container_id
fi
docker run -d -p $host_port:$container_port --name $container_name $image_name
echo "-----------------------------------------------------------Run container successfully----------------------------------------------------------------------------------------------"
#echo "-----------------------------------------------------------Simple health check by actuator,you can define health check by yourself,for exp: check db,depander service etc.---------"

## http://localhost:$host_port/actuator/health
#i=1
#while [ $i -le 30 ]
#do
 # let i++
  #health_ret = `curl http://127.0.0.1:$host_port/actuator/health`
  #if [ $health_ret = '{"status":"UP"}' ]; then
#       echo "--------------------------------------------------------------------Health check is good!--------------------------------------------------------------------------------"
 # fi
  #sleep 100
#done
#echo $i;

echo "------------------------------------------------------------Push image to remote repositorie(Here is Aliyun)-----------------------------------------------------------------------"
version=cicd-demo-$(date "+%Y%m%d%H%M%S")
docker login --username=465230373@qq.com -p=Milenfan123 registry.cn-shenzhen.aliyuncs.com
docker tag 106d9e1f143a registry.cn-shenzhen.aliyuncs.com/k8simages_jo/cicd:$version
docker push registry.cn-shenzhen.aliyuncs.com/k8simages_jo/cicd:$version